package sesi.ejava.control;

import sesi.ejava.bcam.R;
import sesi.ejava.settings.BaseSettings;
import sesi.ejava.stream.ControlOutputStream;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * Cette classe permet la gestion de la climatisation
 * 
 * @author dante
 *
 */
public class ClimateControlActivity extends Activity {
	
	public static final byte FAN0 = 0 ;
	public static final byte FAN1 = 1 ;
	public static final byte FAN2 = 2 ;
	public static final byte FAN3 = 3 ;
	public static final byte FAN4 = 4 ;
	
	public static final byte ZONE_HEAD      = 0 ;
	public static final byte ZONE_HEAD_FEET = 1 ;
	public static final byte ZONE_FEET      = 2 ;
	public static final byte ZONE_FEET_WIND = 3 ;
	
	private Button       buttonClimPlus      ;
	private Button   	 buttonClimMinus     ;
	private TextView 	 textViewTgtTemp     ;
	private ToggleButton toggleFanOff        ;
	private ToggleButton toggleFan1          ;
	private ToggleButton toggleFan2          ;
	private ToggleButton toggleFan3          ;
	private ToggleButton toggleFan4          ;
	private ImageButton  imgButtonHead       ;
	private ImageButton	 imgButtonHeadFeet   ;  
	private ImageButton  imgButtonFeet       ;
	private ImageButton  imgButtonFeetWindow ;
	private ImageButton  imgButtonRearWindow ;
	private ImageButton  imgButtonAirExternal;
	private ImageButton  imgButtonAirRecycle ;
	
	private int          targetTemperature   ;
	private boolean      celsius             ; // true = C false = F
	private boolean	     rearWindowHeat      ;
	
	private ControlOutputStream             stream   ;
	private ControlOutputStream.ControlData sendData ;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clim_layout);
	
		// récupere l'instance su flux de  sortie
		stream = ControlOutputStream.getInstance() ;
		
		//sendData = stream.new ControlData() ;
		sendData = ControlDataSingleton.getInstance().getControlData() ;
		
		
		rearWindowHeat       = false;	
        
        buttonClimPlus       = (Button      ) findViewById (R.id.buttonClimPlus      );
        buttonClimMinus      = (Button      ) findViewById (R.id.buttonClimMinus     );
        textViewTgtTemp      = (TextView    ) findViewById (R.id.textViewTgtTemp     );
        toggleFanOff         = (ToggleButton) findViewById (R.id.toggleFanOff        ); 
        toggleFan1           = (ToggleButton) findViewById (R.id.toggleFan1          );
        toggleFan2           = (ToggleButton) findViewById (R.id.toggleFan2          );
        toggleFan3           = (ToggleButton) findViewById (R.id.toggleFan3          );
        toggleFan4           = (ToggleButton) findViewById (R.id.toggleFan4          );
        imgButtonHead        = (ImageButton ) findViewById (R.id.imgButtonHead       );
        imgButtonHeadFeet    = (ImageButton ) findViewById (R.id.imgButtonHeadFeet   );
        imgButtonFeet        = (ImageButton ) findViewById (R.id.imgButtonFeet       );
        imgButtonFeetWindow  = (ImageButton ) findViewById (R.id.imgButtonFeetWindow );
        imgButtonRearWindow  = (ImageButton ) findViewById (R.id.imgButtonRearWindow );
        imgButtonAirExternal = (ImageButton ) findViewById (R.id.imgButtonAirExternal);
        imgButtonAirRecycle  = (ImageButton ) findViewById (R.id.imgButtonAirRecycle );
        
        buttonClimPlus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onButtonClimPlusClicked () ;
			}
		});
        buttonClimMinus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onButtonClimMinusClicked() ;
			}
		});
        toggleFanOff.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleFanOffClicked() ;
			}
		});
        toggleFan1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleFan1Clicked() ;
			}
		});
        toggleFan2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleFan2Clicked() ;
			}
		});
        toggleFan3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleFan3Clicked();
			}
		});
        toggleFan4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleFan4Clicked();
			}
		});
        imgButtonHead.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onImgButtonHeadClicked();
			}
		});
        imgButtonHeadFeet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onImgButtonHeadFeetClicked();
			}
		});
        imgButtonFeet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onImgButtonFeetClicked();
			}
		});
        imgButtonFeetWindow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onImgButtonFeetWindowClicked();
			}
		});
        imgButtonRearWindow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onImgButtonRearWindowClicked();
			}
		});
        imgButtonAirExternal.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onImgButtonAirExternalClicked();
			}
		});
        imgButtonAirRecycle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onImgButtonAirRecycleClicked();
			}
		});
        
        // états initiaux des toggleButton
        toggleFanOff.setChecked(true ) ;
        toggleFan1.setChecked  (false) ;
        toggleFan2.setChecked  (false) ;
        toggleFan3.setChecked  (false) ;
        toggleFan4.setChecked  (false) ;
        
     // TODO : check si celsius ou farenheit
        celsius = BaseSettings.getsettingsSingleton(this).getTUnit() ;
     	if (celsius) {
     		targetTemperature = 20 ;
     		textViewTgtTemp.setText(targetTemperature+"°C") ;
     	}else {
     		targetTemperature = 68 ;
     		textViewTgtTemp.setText(targetTemperature+"°F") ;
     		// TODO faire un refresh en farheineit	
     	}
	}
	
	private void onButtonClimPlusClicked () {
    	targetTemperature ++ ;
    	if (celsius == true ) {
    		textViewTgtTemp.setText(targetTemperature+"°C") ;
    	} else {
    		textViewTgtTemp.setText(targetTemperature+"°F") ;
    	}
    	sendData.setTemperature((short)targetTemperature) ;
    	stream.sendData(sendData);
    }
	private void onButtonClimMinusClicked () {
		targetTemperature -- ;
		if (celsius == true ) {
    		textViewTgtTemp.setText(targetTemperature+"°C") ;
    	} else {
    		textViewTgtTemp.setText(targetTemperature+"°F") ;
    	}
		sendData.setTemperature((short)targetTemperature) ;
    	stream.sendData(sendData);
	}
	private void onToggleFanOffClicked () {
		// desactiver les autres boutons "FAN"
		toggleFanOff.setChecked(true ) ;
        toggleFan1.setChecked  (false) ;
        toggleFan2.setChecked  (false) ;
        toggleFan3.setChecked  (false) ;
        toggleFan4.setChecked  (false) ;
		// envoyer la trame
        sendData.setFanSpeed(FAN0) ;
    	stream.sendData(sendData);
	}
	private void onToggleFan1Clicked () {
		// desactiver les autres boutons "FAN"
		toggleFanOff.setChecked(false) ;
        toggleFan1.setChecked  (true ) ;
        toggleFan2.setChecked  (false) ;
        toggleFan3.setChecked  (false) ;
        toggleFan4.setChecked  (false) ;
		// envoyer la trame
        sendData.setFanSpeed(FAN1) ;
    	stream.sendData(sendData);
	}
	private void onToggleFan2Clicked () {
		// desactiver les autres boutons FAN
		toggleFanOff.setChecked(false) ;
        toggleFan1.setChecked  (true ) ;
        toggleFan2.setChecked  (true ) ;
        toggleFan3.setChecked  (false) ;
        toggleFan4.setChecked  (false) ;
		// envoyer trame
        sendData.setFanSpeed(FAN2) ;
    	stream.sendData(sendData);
	}
	private void onToggleFan3Clicked () {
		// desactiver les autres boutons FAN
		toggleFanOff.setChecked(false) ;
        toggleFan1.setChecked  (true ) ;
        toggleFan2.setChecked  (true ) ;
        toggleFan3.setChecked  (true ) ;
        toggleFan4.setChecked  (false) ;
		// envoyer trame
        sendData.setFanSpeed(FAN3) ;
    	stream.sendData(sendData);
	}
	private void onToggleFan4Clicked () {
		// desactiver les autres boutons FAN
		toggleFanOff.setChecked(false) ;
        toggleFan1.setChecked  (true ) ;
        toggleFan2.setChecked  (true ) ;
        toggleFan3.setChecked  (true ) ;
        toggleFan4.setChecked  (true ) ;
		// envoyer trame
        sendData.setFanSpeed(FAN4) ;
    	stream.sendData(sendData);
	}
	
	private void onImgButtonHeadClicked (){
		// activer head et desactiver les autres boutons ZONE
		imgButtonHead.setColorFilter(Color.RED) ;
		imgButtonHeadFeet.clearColorFilter();
		imgButtonFeet.clearColorFilter() ;
		imgButtonFeetWindow.clearColorFilter();
		// envoyer la trame
		sendData.setFanZone(ZONE_HEAD) ;
    	stream.sendData(sendData);
	}
	private void onImgButtonHeadFeetClicked () {
		// desactiver les autres boutons ZONE
		imgButtonHead.clearColorFilter();
		imgButtonHeadFeet.setColorFilter(Color.RED) ;
		imgButtonFeet.clearColorFilter();
		imgButtonFeetWindow.clearColorFilter();
		// envoyer la trame
		sendData.setFanZone(ZONE_HEAD_FEET) ;
    	stream.sendData(sendData);
	}
	private void onImgButtonFeetClicked () {
		// desactiver les autres boutons ZONE
		imgButtonHead.clearColorFilter() ;
		imgButtonHeadFeet.clearColorFilter();
		imgButtonFeet.setColorFilter(Color.RED) ;
		imgButtonFeetWindow.clearColorFilter();
		// envoyer la trame
		sendData.setFanZone(ZONE_FEET) ;
    	stream.sendData(sendData);
	}
	private void onImgButtonFeetWindowClicked () {
		// desactiver les autres boutons ZONE
		imgButtonHead.clearColorFilter() ;
		imgButtonHeadFeet.clearColorFilter() ;
		imgButtonFeet.clearColorFilter() ;
		imgButtonFeetWindow.setColorFilter(Color.RED);
		// envoyer la trame
		sendData.setFanZone(ZONE_FEET_WIND) ;
    	stream.sendData(sendData);
	}
	private void onImgButtonRearWindowClicked () {
		// TODO envoyer la trame
		if (rearWindowHeat == false) {
			imgButtonRearWindow.setColorFilter(Color.RED);
			rearWindowHeat = true ;
		} else {
			imgButtonRearWindow.clearColorFilter() ;
			rearWindowHeat = false ;
		}
		sendData.setRearDefrost(rearWindowHeat);
    	stream.sendData(sendData);
	}
	private void onImgButtonAirExternalClicked () {
		// desactiver le bouton AIR_RECYCLE
		imgButtonAirRecycle.clearColorFilter() ;
		// activer le bouton AIR_EXTERNAL
		imgButtonAirExternal.setColorFilter(Color.RED) ;
		// envoyer la trame
		sendData.setAirEntrance(true);
    	stream.sendData(sendData);
	}
	private void onImgButtonAirRecycleClicked () {
		// activer le bouton AIR_RECYCLE
		imgButtonAirRecycle.setColorFilter(Color.RED) ;
		// desactiver le bouton AIR_EXTERNAL
		imgButtonAirExternal.clearColorFilter() ;
		// envoyer la trame
		sendData.setAirEntrance(false);
    	stream.sendData(sendData);
	}
	
	/**
	 * Récupere l'état courrant du système
	 */
	public void getCurrentState () {
		
	}
	
	/**
	 * Pour initialiser les boutons
	 *//*
	private void setButtons () {
	
	}*/
	
	/**
	 * pour initialiser la température cible
	 * @param la nouvelle température
	 *//*
	private void setTemp (int temp) {
		
	}*/
	protected void onStart() {
		super.onStart();
		
		byte    fanSpeed = sendData.getFanSpeed   ();
		byte    fanZone  = sendData.getFanZone    ();
		boolean rearWind = sendData.isRearDefrost ();
		boolean airExt   = sendData.isAirEntrance ();
		short   temp     = sendData.getTemperature();
		
		textViewTgtTemp.setText(Short.toString(temp));
		
		if (rearWind == true) {
			imgButtonRearWindow.setColorFilter(Color.RED) ;
		} else {
			imgButtonRearWindow.clearColorFilter() ;
		}
		
		if (airExt == true) {
			imgButtonAirExternal.setColorFilter(Color.RED) ;
			imgButtonAirRecycle.clearColorFilter() ;
		} else if (airExt == false) {
			imgButtonAirExternal.clearColorFilter() ;
			imgButtonAirRecycle.setColorFilter(Color.RED) ;
		} else {
			imgButtonAirExternal.clearColorFilter() ;
			imgButtonAirRecycle.clearColorFilter() ;
		}
		
		if ((int)fanZone == ZONE_HEAD) {
			imgButtonHead.setColorFilter(Color.RED);
			imgButtonHeadFeet.clearColorFilter(); 
			imgButtonFeet.clearColorFilter();
			imgButtonFeetWindow.clearColorFilter();
		} else if ((int)fanZone == ZONE_HEAD_FEET) {
			imgButtonHead.clearColorFilter();
			imgButtonHeadFeet.setColorFilter(Color.RED); 
			imgButtonFeet.clearColorFilter();
			imgButtonFeetWindow.clearColorFilter();
		} else if ((int)fanZone == ZONE_FEET) {
			imgButtonHead.clearColorFilter();
			imgButtonHeadFeet.clearColorFilter(); 
			imgButtonFeet.setColorFilter(Color.RED);
			imgButtonFeetWindow.clearColorFilter();
		} else if ((int)fanZone == ZONE_FEET_WIND) {
			imgButtonHead.clearColorFilter();
			imgButtonHeadFeet.clearColorFilter();
			imgButtonFeet.clearColorFilter();
			imgButtonFeetWindow.setColorFilter(Color.RED);
		}else {
			imgButtonHead.clearColorFilter();
			imgButtonHeadFeet.clearColorFilter(); 
			imgButtonFeet.clearColorFilter();
			imgButtonFeetWindow.clearColorFilter();
		}
		
		if ((int)fanSpeed == FAN0) {
			toggleFanOff.setChecked(true );
			toggleFan1.setChecked  (false);
			toggleFan2.setChecked  (false);
			toggleFan3.setChecked  (false);
			toggleFan4.setChecked  (false);
		}else if ((int)fanSpeed == FAN1) {
			toggleFanOff.setChecked(false);
			toggleFan1.setChecked  (true );
			toggleFan2.setChecked  (false);
			toggleFan3.setChecked  (false);
			toggleFan4.setChecked  (false);
		}else if ((int)fanSpeed == FAN2) {
			toggleFanOff.setChecked(false);
			toggleFan1.setChecked  (true );
			toggleFan2.setChecked  (true );
			toggleFan3.setChecked  (false);
			toggleFan4.setChecked  (false);
		}else if ((int)fanSpeed == FAN3) {
			toggleFanOff.setChecked(false);
			toggleFan1.setChecked  (true );
			toggleFan2.setChecked  (true );
			toggleFan3.setChecked  (true );
			toggleFan4.setChecked  (false);
		}else if ((int)fanSpeed == FAN4){
			toggleFanOff.setChecked(false);
			toggleFan1.setChecked  (true );
			toggleFan2.setChecked  (true );
			toggleFan3.setChecked  (true );
			toggleFan4.setChecked  (true );
		} else {
			toggleFanOff.setChecked(false);
			toggleFan1.setChecked  (false);
			toggleFan2.setChecked  (false);
			toggleFan3.setChecked  (false);
			toggleFan4.setChecked  (false);
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
}
